const global = this;
global.console = {
    log: print
};
(function () {
    const MAX_TIMEOUT = 0x40000000;
    const LOOP_PERIOD_IN_MS = 20;
    const sleepMs = os.sleepMs;
    const getTickCountMs = os.getTickCountMs;
    var pendingTimeoutList = [];
    var intervalList = [];
    var periodCount = 0;
    global.setTimeout = function (handler, timeout, arg) {
        timeout = timeout || 0;
        if (timeout > MAX_TIMEOUT) {
            timeout = MAX_TIMEOUT;
        }
        var fireAtTick = getTickCountMs() + timeout;
        pendingTimeoutList.push([fireAtTick, handler, arg]);
    };
    global.setInterval = function (handler, timeout, arg) {
        timeout = timeout || 0;
        var loopCount = ~~(timeout / LOOP_PERIOD_IN_MS);
        if (loopCount <= 0) {
            loopCount = 1;
        }
        intervalList.push([loopCount, handler, arg]);
    };
    function handleTimeouts() {
        while (true) {
            var i;
            for (i = 0; i < pendingTimeoutList.length; i++) {
                var diff = pendingTimeoutList[i][0] - getTickCountMs();
                if ((diff < 0) || (diff > MAX_TIMEOUT)) {
                    break;
                }
            }
            if (i >= pendingTimeoutList.length) {
                break;
            }
            var t = pendingTimeoutList[i];
            pendingTimeoutList.splice(i, 1);
            t[1].call(global, t[2]);
        }
    }
    function handleIntervals() {
        for (var i = 0; i < intervalList.length; i++) {
            var t = intervalList[i];
            if ((periodCount % t[0]) == 0) {
                t[1].call(global, t[2]);
            }
        }
    }
    global.mainLoop = function () {
        while (true) {
            periodCount++;
            handleTimeouts();
            handleIntervals();
            sleepMs(LOOP_PERIOD_IN_MS);
        }
    };
})();
spiffs.mount();
if (file.exists('/c/boot.js')) {
    var bootjsFile = file.open('/c/boot.js', 'r');
    print("eval boot.js...");
    var bootjs = bootjsFile.read();
    bootjsFile.close();
    bootjsFile = undefined;
    eval(bootjs);
    bootjs = undefined;
}
/*
uart.initPort(0,
    uart.UART_PIN_NO_CHANGE,
    uart.UART_PIN_NO_CHANGE,
    uart.UART_PIN_NO_CHANGE,
    uart.UART_PIN_NO_CHANGE);


setInterval(function(){
    bridge.pollDebugEvent(0);
}, 100);*/